///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IMAGE_ALGORITHM_THRESHOLD_H_
#define _BUOLA_IMAGE_ALGORITHM_THRESHOLD_H_

#include <buola/image/algorithm/detail/opencv.h>

namespace buola { namespace img {

//! \name thresholding
//@{

template<typename tF1,typename tF2>
inline void threshold(const CView<tF1> &pSrc,const CMView<tF2> &pDst,double pThreshold,int pMax=255,
                      int pType=cv::THRESH_BINARY)
{
    cv::threshold(mat_wrap(pSrc),mat_wrap(pDst),pThreshold,pMax,pType);
}

inline void threshold_rgb(const CView_rgb8 &pSrc,const CMView_gray8 &pDst,
                          int pThreshold=30)
{
    for(int y=0;y<pSrc.Height();++y)
    {
        auto lSrcIt=pSrc.Row(y).begin();
        auto lDstIt=pDst.Row(y).begin();

        for(int x=0;x<pSrc.Width();++x)
        {
            if(lSrcIt[x][0]>pThreshold||lSrcIt[x][1]>pThreshold||lSrcIt[x][2]>pThreshold)
            {
                lDstIt[x]=255;
            }
            else
            {
                lDstIt[x]=0;
            }
        }
    }
}

inline void threshold_rgb_sq(const CView_rgb8 &pSrc,const CMView_gray8 &pDst,
                          int pThreshold=30)
{
    for(int y=0;y<pSrc.Height();++y)
    {
        auto lSrcIt=pSrc.Row(y).begin();
        auto lDstIt=pDst.Row(y).begin();

        for(int x=0;x<pSrc.Width();++x)
        {
            if((int)lSrcIt[x][0]*(int)lSrcIt[x][0]+
                    (int)lSrcIt[x][1]*(int)lSrcIt[x][1]+
                    (int)lSrcIt[x][2]*(int)lSrcIt[x][2]>pThreshold)

            {
                lDstIt[x]=255;
            }
            else
            {
                lDstIt[x]=0;
            }
        }
    }
}

inline void threshold_rgb_sq_not(const CView_rgb8 &pSrc,const CMView_gray8 &pDst,
                          int pThreshold=30)
{
    for(int y=0;y<pSrc.Height();++y)
    {
        auto lSrcIt=pSrc.Row(y).begin();
        auto lDstIt=pDst.Row(y).begin();

        for(int x=0;x<pSrc.Width();++x)
        {
            if((int)lSrcIt[x][0]*(int)lSrcIt[x][0]+
                    (int)lSrcIt[x][1]*(int)lSrcIt[x][1]+
                    (int)lSrcIt[x][2]*(int)lSrcIt[x][2]>pThreshold)

            {
                lDstIt[x]=0;
            }
            else
            {
                lDstIt[x]=255;
            }
        }
    }
}

//@}

/*namespace image*/ } /*namespace buola*/ }

#endif
